package com.healthcloud.controller.modules.index;

import com.healthcloud.dto.res.CountInstrumentMonthDto;
import com.healthcloud.service.HlHospitalService;
import com.healthcloud.service.index.CountService;
import com.healthcloud.utils.ApiRes;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/count")
public class CountController {

    @Autowired
    private CountService countService;

    @Autowired
    private HlHospitalService hlHospitalService;

    @RequestMapping(value = "/toCount", method = RequestMethod.GET)
    public String toCount() {
        return "bus/count/count";
    }

    /*@ApiOperation(value = "使用情况统计", notes = "patientNum:2018年至当年病人总数 patientNewNum:30天新增人数 " +
            "instrumentUseAllNum:设备使用次数 tenUseMsg:近10次使用人 instrumentUseTodayNum:今天设备使用情况" +
            "instrumentActiveTodayNum:活跃设备")
    @RequestMapping(value = "/useCount", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes useCount(@ApiParam(value = "0:全部 1:高血压 2:糖尿病", name = "type")
                            @RequestParam(value = "type", required = false) String type) {
        String ids = hlHospitalService.getChildeIds();
        Integer patientNum = countService.patientNum(ids, type);
        Integer patientNewNum = countService.patientNewNum(ids, type);
        //0:全部  1:今天
        Integer instrumentUseAllNum = countService.instrumentUseNum(ids, type, "0");
        List<Map> tenUseMsg = countService.tenUseMsg(ids, type);
        Integer instrumentUseTodayNum = countService.instrumentUseNum(ids, type, "1");
        Integer instrumentActiveTodayNum = countService.instrumentActiveNum(ids, type, "1");

        Map<String, Object> data = new HashMap<>();
        data.put("patientNum", patientNum);
        data.put("patientNewNum", patientNewNum);
        data.put("instrumentUseAllNum", instrumentUseAllNum);
        data.put("tenUseMsg", tenUseMsg);
        data.put("instrumentUseTodayNum", instrumentUseTodayNum);
        data.put("instrumentActiveTodayNum", instrumentActiveTodayNum);
        return new ApiRes(data);
    }*/

    @ApiOperation(value = "使用情况统计", notes ="instrumentNumber:2018年总仪器数量 instrumentNewNum:近30天新增仪器数量" +
            "instrumentUseAllNum:设备使用次数 tenUseMsg:近10次使用人 instrumentUseTodayNum:今天设备使用情况" +
            "instrumentActiveTodayNum:活跃设备")
    @RequestMapping(value = "/useInstrumentCount", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes useInstrumentCount(@ApiParam(value = "0:全部 1:高血压 2:糖尿病", name = "type")
                           @RequestParam(value = "type", required = false) String type){
        String ids = hlHospitalService.getChildeIds();
        Integer instrumentNumber = countService.instrumentNumber(ids, type);
        Integer instrumentNewNum = countService.instrumentNewNum(ids, type);
        //0:全部  1:今天
        Integer instrumentUseAllNum = countService.instrumentUseNum(ids, type, "0");
        List<Map> tenUseMsg = countService.tenUseMsg(ids, type);
        Integer instrumentUseTodayNum = countService.instrumentUseNum(ids, type, "1");
        Integer instrumentActiveTodayNum = countService.instrumentActiveNum(ids, type, "1");

        Map<String, Object> data = new HashMap<>();
        data.put("instrumentNumber", instrumentNumber);
        data.put("instrumentNewNum", instrumentNewNum);
        data.put("instrumentUseAllNum", instrumentUseAllNum);
        data.put("tenUseMsg", tenUseMsg);
        data.put("instrumentUseTodayNum", instrumentUseTodayNum);
        data.put("instrumentActiveTodayNum", instrumentActiveTodayNum);
        return new ApiRes(data);
    }






    @ApiOperation(value = "使用情况统计", notes = "instrumentUse:设备使用次数 instrumentActive:活跃设备数 ")
    @RequestMapping(value = "/instrumentUse", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes instrumentUse(@ApiParam(value = "1:高血压 2:糖尿病", name = "type")
                                 @RequestParam(value = "type", required = false) String type,
                                @ApiParam(value = "0:全部 1:日 2:周 3:月 4:季 5:半年 6:年", name = "dateType")
                                 @RequestParam(value = "dateType", required = false) String dateType) {

        String ids = hlHospitalService.getChildeIds();
        List<Map> instrumentUseDate = countService.instrumentUseDate(ids, type, dateType);
        List<Map> instrumentActiveDate = countService.instrumentActiveDate(ids, type, dateType);

        Map<String, Object> data = new HashMap<>();
        data.put("instrumentUseDate", instrumentUseDate);
        data.put("instrumentActiveDate", instrumentActiveDate);
        return new ApiRes(data);
    }

    @ApiOperation(value = "建档人数统计")
    @RequestMapping(value = "/patientMonth", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes patientMonth(@ApiParam(value = "1:高血压 2:糖尿病 3:高血脂 4:骨松", name = "type")
                                @RequestParam(value = "type", required = false) String type,
                               @ApiParam(value = "年份", name = "year")
                                @RequestParam(value = "year", required = false) String year) {

        String ids = hlHospitalService.getChildeIds();
        List<Map> patientMonth = countService.patientMonth(ids, type, year);

        return new ApiRes(patientMonth);
    }

    /**
     * 按月统计设备绑定数量（统计包括慢病后台绑定 + 积分商城绑定）
     *
     * @param type
     * @param year
     * @return 该接口统计有问题。设备绑定没有对应身份证号，或者患者类型不对。需要关联 pp_patient_base 患者表。
     */
    @ApiIgnore
    @ApiOperation(value = "按月统计设备绑定数量")
    @RequestMapping(value = "/instrumentMonth", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes instrumentMonth(@ApiParam(value = "1:血压计 2:血糖仪", name = "type")
                                   @RequestParam(value = "type", required = false) String type,
                                  @ApiParam(value = "年份", name = "year")
                                   @RequestParam(value = "year", required = false) String year) {

        String ids = hlHospitalService.getChildeIds();
        List<Map> instrumentMonth = countService.instrumentMonth(ids, type, year);

        return new ApiRes(instrumentMonth);
    }

    /**
     * 按月统计设备绑定数量（慢病后台绑定 和 积分商城绑定 分别统计）
     * <p>
     * update_time 为空 create_time
     * update_time 不为空 update_time
     * <p>
     * integral_hosp_id 为空 后台
     * integral_hosp_id 不为空 积分商城
     */
    @ApiOperation(value = "按月统计设备绑定数量，区分慢病后台绑定 和 积分商城绑定")
    @RequestMapping(value = "/countInstrumentMonth", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes<List<CountInstrumentMonthDto>> countInstrumentMonth(
            @ApiParam(value = "1:血压计 2:血糖仪", name = "type") @RequestParam(value = "type", required = false) String type,
            @ApiParam(value = "年份", name = "year") @RequestParam(value = "year", required = false) String year
    ) {

        String ids = hlHospitalService.getChildeIds();
        if ("3".equals(type) || "4".equals(type)) { // 血脂和骨松没有设备统计。
            return new ApiRes(null);
        }
        List<CountInstrumentMonthDto> list = countService.countInstrumentMonth(ids, type, year);
        return new ApiRes(list);
    }

    @ApiOperation(value = "按月统计慢病新增人数")
    @RequestMapping(value = "/newPatientMonth", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes newPatientMonth(@ApiParam(value = "1:高血压 2:糖尿病 3:高血脂 4:骨松", name = "type")
                                   @RequestParam(value = "type", required = false) String type,
                                  @ApiParam(value = "年份", name = "year")
                                   @RequestParam(value = "year", required = false) String year) {

        String ids = hlHospitalService.getChildeIds();
        List<Map> accompanyMonth = countService.newPatientMonth(ids, type, year);

        return new ApiRes(accompanyMonth);
    }

    @ApiOperation(value = "随访月度统计")
    @RequestMapping(value = "/accompanyMonth", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes accompanyMonth(@ApiParam(value = "1:高血压 2:糖尿病 3:高血脂 4:骨松", name = "type")
                                  @RequestParam(value = "type", required = false) String type,
                                 @ApiParam(value = "年份", name = "year")
                                  @RequestParam(value = "year", required = false) String year) {

        String ids = hlHospitalService.getChildeIds();
        List<Map> accompanyMonth = countService.accompanyMonth(ids, type, year);

        return new ApiRes(accompanyMonth);
    }

}
